<?php

namespace App\Exports;

use App\Models\Agent;
use App\Models\AgentTopUpOrder;
use App\Models\PaymentOrder;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Maatwebsite\Excel\Concerns\FromQuery;

class AgentRechargeRecordExport extends BaseExport implements FromQuery
{
    public function headings(): array
    {
        return [
            '序号',
            '充值/代付单号',
            '代理名称',
            '代理手机号',
            '等级',
            '加入时间',
            '直接/间接推荐人',
            '关联合伙人',
            '支付通道',
            '充值金额',
            '充值时间',
            '状态',
        ];
    }

    public function map($row): array
    {
        $row = $row->toArray();

        return [
            $row['id'], // 序号
            $row['order_id'], // '充值/代付单号
            $this->removeUncommonWords($this->removeEmoji($row['agent']['name'])), // 代理名称
            $row['agent']['phone'], // 代理手机号
            Agent::LEVEL_MAP[$row['agent']['level']]['text'], // 等级
            $row['agent']['created_at'], // 加入时间
            $this->removeUncommonWords($this->removeEmoji(($row['direct_inviter']['name'] ?? '-') . '/' . ($row['indirect_inviter']['name'] ?? '-'))), // '直接间接推荐人
            $this->removeUncommonWords($this->removeEmoji($row['agent']['affiliated_partner']['name'] ?? '')), // 关联合伙人
            PaymentOrder::PAYMENT_CHANNEL_MAP[$row['payment_orders'][0]['payment_channel']], // 支付通道
            $row['amount'], // 充值金额
            $row['created_at'], // 充值时间
            AgentTopUpOrder::STATUS_MAP[$row['status']], // 状态
        ];
    }

    /**
     * @return \Illuminate\Database\Query\Builder
     */
    public function query()
    {
        return AgentTopUpOrder::query()
            ->with(['agent' => function ($builder) {
                $builder->with(['directInviter', 'indirectInviter', 'affiliatedPartner']);
            }, 'paymentOrders'])
            ->when(($this->request['name'] ?? '') || ($this->request['phone'] ?? '') || ($this->request['level'] ?? 0), function (Builder $builder) {
                $builder
                    ->join('agents', 'agents.id', '=', 'agent_top_up_orders.agent_id')
                    ->when($this->request['name'] ?? '', function (Builder $builder, $name) {
                        $builder->where('agents.name', 'like', "%$name%");
                    })
                    ->when($this->request['phone'] ?? '', function (Builder $builder, $phone) {
                        $builder->where('agents.phone', 'like', "%$phone%");
                    })
                    ->when($this->request['level'] ?? 0, function (Builder $builder, $level) {
                        $builder->where('agents.level', $level);
                    });
            })
            ->when($this->request['status'] ?? 0, function (Builder $builder, $status) {
                $builder->where('agent_top_up_orders.status', $status);
            })
            ->when($this->request['created_at_begin'] ?? '', function (Builder $builder, $createdAtBegin) {
                $builder->where('agent_top_up_orders.created_at', '>=', Carbon::parse($createdAtBegin)->startOfDay());
            })
            ->when($this->request['created_at_end'] ?? '', function (Builder $builder, $createdAtEnd) {
                $builder->where('agent_top_up_orders.created_at', '<=', Carbon::parse($createdAtEnd)->endOfDay());
            })
            ->select(['agent_top_up_orders.*']);
    }
}
